package com.sxt.servlet;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.sxt.common.Constant;
import com.sxt.common.Result;
import com.sxt.pojo.User;
import com.sxt.service.ICustomerService;
import com.sxt.service.impl.CustomerServiceImpl;
import com.sxt.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/customer.do")
public class CustomerServlet extends HttpServlet {

    private ICustomerService customerService = new CustomerServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tag = req.getParameter("service");
        try {
            Method m = this.getClass().getDeclaredMethod(tag,HttpServletRequest.class,HttpServletResponse.class);
            m.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected void page(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        Integer pageNo = 1;
        Integer pageSize = 10;
        //进行数据的校验
        if (NumberUtil.isNumber(page)){
            pageNo = Integer.parseInt(page) > 0 ? Integer.parseInt(page):pageNo;
        }
        if (NumberUtil.isNumber(limit)){
            pageSize = Integer.parseInt(limit) > 0 ? Integer.parseInt(limit):pageSize;
        }
        //查询条件
        Map<String, String[]> parameterMap = req.getParameterMap();
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String company = req.getParameter("company");
        String userId = req.getParameter("userId");
        String deleted = req.getParameter("deleted");
        //存储所有查询参数的容器
        Map<String,String> params = new HashMap<>();
        if (StrUtil.isNotBlank(name)){
            params.put("name",name);
        }
        if (StrUtil.isNotBlank(sex)){
            params.put("sex",sex);
        }
        if (StrUtil.isNotBlank(phone)){
            params.put("phone",phone);
        }
        if (StrUtil.isNotBlank(company)){
            params.put("company",company);
        }
        if (StrUtil.isNotBlank(userId)){
            params.put("userId",userId);
        }
        if (StrUtil.isNotBlank(deleted)){
            params.put("deleted",deleted);
        }
        //查询分页信息
        Result rs = customerService.queryPage(params,pageNo,pageSize);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name= req.getParameter("name");
        String phone = req.getParameter("phone");
        String company = req.getParameter("company");
        String sex = req.getParameter("sex");
        //获取登录的用户
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER_KEY);
        //插入数据
        Result rs = customerService.add(name,phone,sex,company,user.getId());
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 处理修改请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name= req.getParameter("name");
        String phone = req.getParameter("phone");
        String company = req.getParameter("company");
        String sex = req.getParameter("sex");
        //获取登录的用户
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER_KEY);
        //插入数据
        Result rs = customerService.update(id,name,phone,sex,company,user.getId());
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 处理删除请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        //插入数据
        Result rs = customerService.delete (id);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    protected void setSalesman(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //业务员ID
        String userId = req.getParameter("userId");
        //客户ID
        String[] cIds = req.getParameterValues("cId");
        //批量修改客户的业务员ID
        Result rs = customerService.batchSetSalesman(userId,cIds);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 新增拜访记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void visit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //客户ID
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String visitTime = req.getParameter("visitTime");
        String descp = req.getParameter("descp");
        //获取当前登录的用户的id 和 名字
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute(Constant.CURRENT_USER_KEY);
        Integer userId = user.getId();
        String realname = user.getRealname();
        //新增业务员的拜访客户记录
        Result rs = customerService.addVisitLog(id,name,userId,realname,visitTime,descp);
        //返回
        RespUtil.write(resp,rs);
    }

}
